<?php
/* SVN FILE: $Id: html_helper.php 49 2009-08-26 07:02:47Z phamshantuyet@gmail.com $ */
/**
 * HTML Helper: control and generate html tag as select tag, checkbox tag, link tag......
 * 
 * @package helpers
 * @subpackage helpers.   
 * @author tienhc <hatuhn@gmail.com>
 * @copyright 24h <@see http://24h.com.vn>
 * @version 2009
 * @access public
 */
class HtmlHelper  {
	
	private static  $__instance;
	
	/**
	 * @var string $__current store current url.
	 * 
	 * @access private.
	 */
	private $__current;
	
	/**
 	 * get a Instance of HtmlHelper
     * 
     * @access public 
     * @static
	 */
	public static function getInstance(){
		if(!self::$__instance){
			self::$__instance = new HtmlHelper();		
		}
		return self::$__instance;
	}
	
	/**
   	 * generate image tag.
   	 */
	public function image($path, $attributes=array()){
		return '<img src="'.IMAGE_URL.$path.'" '
				. $this->parserAttribute($attributes).' >';
			
	}
	
	/**
	 * generate link tag, using for load css file.
	 * 
	 * @param
	 * @access public
	 * @static 
	 * @return string.
	 */
	public function css($file){
		return '<link rel="stylesheet" type="text/css" href="'.BASE_URL.'css/'.$file.'.css">';
	}
	
	/**
	 *
	 */
	public function js($file){
		return '<script type="text/javascript" src="'.BASE_URL.'js/'.$file.'.js"></script>';	
	}
	
	/**
	 * create a link tag.
	 * 
	 * @param string $text 
	 * @param string $path
	 * @param array $attributes.
	 * @return string
	 */
	public function link($text, $path, $attributes=array()){ 
		return '<a href="'.$this->url($path).'" '
				.$this->parserAttribute($attributes).'>'
				. $text
				.'</a>';		
	}
	
	/**
	 * create full path url, and has detected protocol's type (https|https)
	 * 
	 * @param string $path.
	 * @access public.
	 * @return string
	 */
	public function url($path){
		// detech ssl
		$out = BASE_URL.str_replace('//', '/',$path);

		return $out;	
	}
	
	/**
	 * create a checkbox tag
	 * 
	 * @param array $data data has format: array($value=>$text) or array($index=>array($key=>$value) )
	 * @param array $attributes.
	 * @param array $display using for custom display text and value of option element.
	 * @access public.
	 * @return string;
	 */
	public function checkbox( $name, $value=null, $ischeck=true, $atttributes=array()){
		return '<input type="checkbox" name="'.$name.'" ' 
				. $this->parserAttributes($attributes) .' value="'.$value.'" '.($ischeck?'checked="checked"':'').'>';
	}
	
	/**
	 * create a select tag.
	 * 
	 * @param array $data data has format: array($value=>$text) or array($index=>array($key=>$value) )
	 * @param array $attributes.
	 * @param array $display using for custom display text and value of option element.
	 * @access public.
	 * @return string;
	 */
	public  function select($data, $selected='', $attributes=array(), $display=null){
		$string = '<select '. self::parserAttribute($attributes).' >';
		if(!empty($data)){
			if(isset($display['blank'])){
				$string .= '<option value=""> ---------------- </option>';	
			}
			foreach($data as $key => $value){ 
				if(isset($display['value']) && isset($display['text'])){ 
					$string .= '<option value="'.$value[$display['value']].'"';
					if($selected == $value[$display['value']]){
						$string .= ' selected="selected" ';		
					}
					$string .= '>'.$value[$display['text']].'</option>';	

				} else {
					$string .= '<option value="'.$value.'"';
					if($selected == $key){
						$string .= ' selected="selected" ';		
					}
					$string .= '>'.$value.'</option>';	
				}
			}	
		}
		$string .= '</select>';
		return $string;
	}
	
	public function input($name, $value='', $attributes=array()){
		return '<input name="'.$name.'" value="'.$value.'" '. $this->parserAttributes($attributes).'>';
	}	
	/**
	 * create a textarea tag.
	 * 
	 * @param string $text
	 * @param array $attributes.
	 * @access public.
	 * @return string;
	 */
	public function textarea($name, $text, $attributes){
		return '<textarea name="'.$name.'" ' . $this->parserAttribute($attributes) . '>' . $text . '</textarea>';
	}
	
	/**
	 * parser and build attribute for html tag.
	 * 
	 * @param array $attributes
	 * @access public.
	 * @return string
	 */
	public  function parserAttribute($attributes){
		if(empty($attributes)) return ;
		 
		$string = '';
		foreach($attributes as $attribute => $value ){
			$string .= $attribute . '="'.$value.'"';	
		}	
		return $string;
	}
}
